package copyproperties;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

public class CopyPropertiesTest {

    private static final Logger log = LoggerFactory.getLogger(CopyPropertiesTest.class);

    private static void test1() {
        test2();
    }

    private static void test2() {
        test3();
    }

    private static void test3() {
        try {
            System.out.println("打印日志...");
            int i = 1 / 0;
        } catch (Exception e) {
            log.error("出错啦");
            log.error(e.getMessage());
        }
        // throw new RuntimeException("出错误了");
    }

    private static void test4() {
        boolean empty = StringUtils.isEmpty("");
        System.out.println("empty = " + empty);

    }

    private static void copyPropertiesTest1() {
        User user = new User("sunhongguang", "baoan", "男", "test");
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto, "sex");
        System.out.println("userDto = " + userDto);
    }

    private static void copyTest2() {
        UserPayment userPayment = new UserPayment();
        userPayment.setTradeNo("126");
        userPayment.setUsername("shg1");
        userPayment.setAmount(new BigDecimal(100000001));
        userPayment.setSucceed(true);
        userPayment.setPaymentDate(new Date());
        userPayment.setPaymentMethod(PaymentMethod.ALIPAY);
        userPayment.setChannelNumber("001");
        userPayment.setState(UserPayment.PaymentState.PROCESSED);
        userPayment.setInvoiceTitle("invoiceTitle");
        userPayment.setTransId("001");
        userPayment.setOperator("System");
        userPayment.setPayType(UserPayment.PayType.FIRSTCHARGE);
        userPayment.setTradeDate(new Date());
        userPayment.setPayUserName("shg");
        userPayment.setCardNumber("001");
        userPayment.setAccountsBank("1000000000000000");
        userPayment.setSequenceNumber("001");
        userPayment.setPrepayId("001");

        UserPaymentDto userPaymentDto = new UserPaymentDto();
        BeanUtils.copyProperties(userPayment, userPaymentDto);
        System.out.println("userPaymentDto = " + userPaymentDto);
        userPaymentDto.setContactWay("1111");
        System.out.println(userPaymentDto + "..");
        System.out.println(userPaymentDto.getCardNumber());

    }


    public static void copyTest3() {
        Person person = new Person();
        person.setName("xxx");
        person.setAge(12);

        Student student = new Student();
        student.setTest("yyy");

        BeanUtils.copyProperties(person, student);

        System.out.println(student.getName());
        System.out.println(student.getAge());
    }

    private static void dateTest() {
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime.atZone(ZoneId.systemDefault());
        System.out.println(localDateTime);
    }

    private static void retainAllTest() {
        Set<String> stringSet = new HashSet<>();
        stringSet.add("1");
        stringSet.add("2");
        stringSet.add("3");
        stringSet.add("4");

        Set<String> stringSet1 = new HashSet<>();
        stringSet1.add("1");
        stringSet1.add("2");
        stringSet1.add("3");

        stringSet.retainAll(stringSet1);
        stringSet.forEach(System.out::println);

    }

    private static void mapTest() {
        Map<String, Person> personMap = new HashMap<>();
        personMap.put("p1", new Person("z1", 12));
        personMap.put("p2", new Person("z2", 13));
        personMap.put("p3", new Person("z3", 14));

        Set<Map.Entry<String, Person>> entries = personMap.entrySet();
        for (Map.Entry<String, Person> entry : entries) {
            System.out.println("entry.getKey() = " + entry.getKey());
            System.out.println("entry.getValue() = " + entry.getValue());
        }
        System.out.println("----------");
        entries.forEach(System.out::println);

        long count = personMap.entrySet()
                .stream()
                .filter(stringPersonEntry -> "z2".equals(Optional.ofNullable(stringPersonEntry.getValue()).orElse(new Person()).getName()))
                .count();
        System.out.println("count = " + count);

    }

    private static void testLong() {
        long l = 10590623744l;
       // int x = 10590623744/1073741824;
        int x1 = 10 / 2;
    }

    public static void main(String[] args) {
       // copyPropertiesTest1();
       // test1();
       // copyTest2();
       // test4();
       // copyTest3();
       //  dateTest();
       //  retainAllTest();
        mapTest();
    }
}
